<?php
$loader->requireOnce('includes/inPatientCore/InPatientCoreErrorCode.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientCoreException.class.php');
$loader->requireOnce('includes/inPatientCore/TestNode.class.php');
$loader->requireOnce('includes/inPatientCore/TestTree.class.php');
$loader->requireOnce('ordo/InPatient.class.php');

// TODO REVIEW Trung Nguyen 2009-07-20
// Missing header class declaration
/**
 * Exception of inpatient core.
 * @package core.includes.inPatientCore
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class TestDiagnosisTicketManagement {
	// TODO REVIEW Trung Nguyen 2009-07-20
	// Using __construct() for constructor
	function __construct() {
		$this->logger = LoggerManager::getLogger('core.inPatientCore.TreatingPatientProcessor');

	}
	/**
	 * Get list of assigned test ticket.
	 * @param $ticketId
	 * @return array().
	 */
	public function listOfTestTicket($ticketId) {
		$testTicket = Celini::newOrdo('TestDiagnosisTicket');
		return $testTicket->listOfAssignedTestTicket($ticketId);
	}

	public static function normalizeData4Display(&$data) {
		TestDiagnosisTicket::normalizeData4Display($data);
			
	}
	
	

	public function buildTestGroupTreeHtml4Desktop($ticketId) {
		$allTests = Celini::newOrdo('Test')->listAllTest();
		$testTree = $this->buildTreeArray($allTests);
		$testTree->buildGroupOfTest4DesktopHtmlList($ticketId);
		return $testTree;
	}
	private function buildTreeArray(&$listOfTests) {
		$testTree = new TestTree();
		foreach ($listOfTests as $item) {
			$newNode = new TestNode($item['test_name'], $item['test_id'], $item['parent_id'], $item);
			$testTree->addNode($newNode);

		}
		return $testTree;
			
	}
	public function buildTestArray4Print($listOfTestItem) {
		$result = array();
		foreach ($listOfTestItem as $testItem) {
			if (!isset($result[$testItem['test_id']])) {
				$result[$testItem['test_id']] = array('test_name'=>$testItem['test_name'], 'test_item'=>array());
			}
			$result[$testItem['test_id']]['test_item'][] = $testItem;
		}
		return $result;

	}

	public function getListOfTestByGroup($testGroupId) {
		$allTests = Celini::newOrdo('TestService')->listTestItem($testGroupId);

		return $allTests;
	}
	/* @param $testTicketInfo
	 * array(ticket_id,requested_time,diagnosis,ticket_id,doctor_id, is_done)
	 * @param $assignedTest
	 * array([test_service_id],...)
	 * @return id of added test ticket.
	 */

	public function addAssignedTest($testTicketInfo, $assignedTest) {
		$testTicket = Celini::newORDO("TestDiagnosisTicket");
		return $testTicket->addAssignedTest($testTicketInfo, $assignedTest);
	}

	public function editAssignedTest($testDiagnosisTicketId, $testTicketInfo, $assignedTest) {
		$testTicket = Celini::newORDO("TestDiagnosisTicket");
		return $testTicket->editAssignedTest($testDiagnosisTicketId, $testTicketInfo, $assignedTest);

	}

	public function deleteTestTicket($testTicketId) {
		$testTicket = Celini::newORDO("TestDiagnosisTicket");
		$testTicket->deleteTestTicket($testTicketId);
		$inTicketItemObj = Celini::newOrdo('InTicketItem');
		$itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
        EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_LAB);
        $inTicketItemObj->deleteItem($testTicketId, $itemType);
	}
	
    public function canDeleteTestTicket($testTicketId) {
        $testTicket = Celini::newORDO("TestDiagnosisTicket");
        return $testTicket->canDeleteTestTicket($testTicketId);
    }
    
    public function isTestTicketDone($testTicketId) {
        $testTicket = Celini::newORDO("TestDiagnosisTicket");
        return $testTicket->isTestTicketDone($testTicketId);
    }
	/**
	 * Get Assigned test ticket.
	 * return array (test_diagnosis_ticket_id, requested_time, diagnosis, PE.name doctor_name, doctor_id, is_done)
	 */
	public function getAssignedTestTicket($testDiagnosisTicketId) {
		$testTicket = Celini::newORDO("TestDiagnosisTicket");

		return $testTicket->getAssignedTestTicket($testDiagnosisTicketId);

	}

	public function updateDoneTestTicket($testDiagnosisTicketId, $isDone) {
		$testTicket = Celini::newORDO("TestDiagnosisTicket");
		$testTicket->setDone($testDiagnosisTicketId, $isDone);
	}

	/**
	 * Get list of assigned test item of a given test ticket id.
	 * @param $testDiagnosisTicketId
	 * @return array(assigned_test_item_id, test_service_id)
	 */
	public function listOfAssignedTestItem($testDiagnosisTicketId) {
		$assigedTestItemObj = Celini::newORDO("AssigedTestItem");
		return $assigedTestItemObj->listOfAssignedTestItem($testDiagnosisTicketId);

	}

	public function saveTestItemResult($ticketId, $testDiagnosisTicketId, $assignedTestDone, $assignedTestNotDone) {

		foreach ($assignedTestDone as $assignedDone) {
			$testResultObj = Celini::newOrdo('AssigedTestItem', $assignedDone);
			$testResultObj->setDone(1);

		}

		foreach ($assignedTestNotDone as $assignedNotDone) {
			$testResultObj = Celini::newOrdo('AssigedTestItem', $assignedNotDone);
			$testResultObj->setDone(0);


		}
		
		$testResultObj = Celini::newOrdo('AssigedTestItem');
		$prices = $testResultObj->countTotalPrices4ATestTicket($testDiagnosisTicketId);
		$itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
        EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_LAB);
	    $inTicketItemObj = Celini::newOrdo('InTicketItem');
	    $inTicketItem = $inTicketItemObj->findInTicket($testDiagnosisTicketId, $itemType);
	    if ($inTicketItem != null) {
	    	$inTicketItemObj->setup($inTicketItem['in_ticket_item_id']);
	    	
	    } else {
	    	$inTicketItemObj->set_id('');	    	
	    }
	    
        $inTicketItemObj->setData($testDiagnosisTicketId,
        $itemType,
        0,$prices['price'],$prices['insurance_price'], $prices['patient_price'], $ticketId, null);
        $inTicketItemObj->persist();
	}



}
?>